require("../UISoundManager.js");
cc.Class({
    extends: cc.Component,
    properties: {
        adIcon: cc.Sprite,
        labName: cc.Label
    },
    onLoad: function () {
        this.adType = 1;
    },
    start: function () {
    },
    setItemData: function (e) {
        if (e && (this.currentAdInfo = e, this.adIconNode(), this.labName)) {
            var t = "";
            e.webpages && e.webpages.length > 0 && (t = e.webpages[0].webpage_name), t && "" != t && (this.labName.string = Global.utils.getFixString(t, 6));
        }
    },
    adIconNode: function () {
        if (this.currentAdInfo) {
            var e = this.currentAdInfo.icon_type, t = this, n = this.adIcon.node;
            switch (n.stopAllActions(), n.removeComponent(cc.Animation), n.setRotation(0), e) {
                case tywx.AdManager.AnimType.STATIC:
                    cc.loader.load({
                        url: t.currentAdInfo.icon_url[0]
                    }, function (e, t) {
                        e || (n.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(t), t && t.width && t.height && n.setContentSize(cc.size(t.width, t.height)));
                    });
                    break;

                case tywx.AdManager.AnimType.SHAKE:
                    cc.loader.load({
                        url: t.currentAdInfo.icon_url[0]
                    }, function (e, t) {
                        n.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(t), t && t.width && t.height && n.setContentSize(cc.size(t.width, t.height)),
                            n.anchorX = .5, n.anchorY = .5;
                        var o = cc.rotateBy(.06, -20), i = cc.rotateBy(.12, 40), a = cc.rotateBy(.12, -40),
                            r = cc.rotateBy(.06, 20), s = cc.delayTime(1);
                        n.runAction(cc.repeatForever(cc.sequence(o, cc.repeat(cc.sequence(i, a), 4), r, s)));
                    });
                    break;

                case tywx.AdManager.AnimType.FRAME:
                    var o = [];
                    cc.loader.load(t.currentAdInfo.icon_url, function (e, i) {
                        if (e) for (var a = 0; a < e.length; a++) cc.log("Error url [" + e[a] + "]: " + i.getError(e[a]));
                        for (a = 0; a < t.currentAdInfo.icon_url.length; a++) if (i.getContent(t.currentAdInfo.icon_url[a])) {
                            var r = new cc.SpriteFrame(i.getContent(t.currentAdInfo.icon_url[a]));
                            o.push(r);
                        }
                        (function () {
                            n.stopAllActions(), n.removeComponent(cc.Animation);
                            var e = o[0].getTexture();
                            e && e.width && e.height && n.setContentSize(cc.size(e.width, e.height));
                            var i = t.currentAdInfo.time_interval, a = i && i > 0 ? 1e3 / i : 10,
                                r = n.addComponent(cc.Animation),
                                s = cc.AnimationClip.createWithSpriteFrames(o, a);
                            s.name = "anim_frame", s.wrapMode = cc.WrapMode.Loop, r.addClip(s), r.play("anim_frame");
                        })();
                    });
            }
        }
    },
    onClickAdIconBtn: function () {
        try {
            var e = this.currentAdInfo.icon_skip_type, t = this.currentAdInfo.toappid,
                n = this.currentAdInfo.togame, o = this.currentAdInfo.path,
                i = this.currentAdInfo.second_toappid;
            console.log("topath ====>" + o);
            var a = this.currentAdInfo.icon_id, r = "0", s = "", l = "0";
            this.currentWebPage && 1 == this.adType && (s = this.currentWebPage.webpage_url,
                r = this.currentWebPage.config_id, l = this.currentWebPage.webpage_id);
            var c = [a, r, s, t, n, l, this.adType];
            if (console.log("bi_paramlist ====> " + JSON.stringify(c)), tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeClickAdBtn, c),
            wx && wx.navigateToMiniProgram) return 1 == e ? void wx.navigateToMiniProgram({
                appId: t,
                path: o || "?from=adcross",
                envVersion: "release",
                extraData: {
                    from: o || "?from=adcross"
                },
                success: function (e) {
                    tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeClickDirectToMiniGameSuccess, c),
                        console.log("wx.navigateToMiniProgram success"), console.log(e);
                },
                fail: function (e) {
                    tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeClickDirectToMiniGameFail, c),
                        console.log("wx.navigateToMiniProgram fail"), console.log(e);
                },
                complete: function (e) {
                    console.log("navigateToMiniProgram ==== complete");
                }
            }) : void (2 == e ? wx.navigateToMiniProgram({
                appId: i,
                path: o || "?from=adcross",
                envVersion: "release",
                extraData: {
                    from: o || "?from=adcross"
                },
                success: function (e) {
                    tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeClickDirectToMiniGameSuccess, c),
                        console.log("wx.navigateToMiniProgram success"), console.log(e);
                },
                fail: function (e) {
                    tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeClickDirectToMiniGameFail, c),
                        console.log("wx.navigateToMiniProgram fail"), console.log(e);
                },
                complete: function (e) {
                    console.log("navigateToMiniProgram ==== complete");
                }
            }) : console.error("Unsupported skip type! Please Check!"));
            if (!this.currentWebPage || !this.currentWebPage.webpage_url) return;
            tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeClickShowQRCode, c),
            tywx.IsWechatPlatform() && wx.previewImage({
                current: [this.currentWebPage.webpage_url],
                urls: [this.currentWebPage.webpage_url],
                success: function (e) {
                    tywx.LOGD(null, "预览图片成功！");
                },
                fail: function (e) {
                    tywx.LOGD(null, "预览图片失败！");
                },
                complete: function (e) {
                    console.log("预览图片完成");
                }
            });
        } catch (e) {
            console.log("error:", "tywx.AdManager.onClickAdIconBtn——" + JSON.stringify(e));
        }
    },
    genRandomSecondAdInfo: function () {
        var e = this, t = this.currentAdInfo.webpages;
        if (void 0 !== t && 0 != t.length) {
            var n = [{
                weight: 0,
                id: "000"
            }];
            for (var o in t) {
                var i = {
                    weight: parseInt(t[o].webpage_weight),
                    id: t[o].config_id
                };
                n.push(i);
            }
            n.sort(function (e, t) {
                return e.weight > t.weight;
            });
            var a = 0;
            n.forEach(function (e) {
                a += e.weight;
            });
            var r = parseInt(1e4 * Math.random()) % (a + 1), s = 0, l = 0, c = null != n ? n.length : 1;
            for (o = 0; o < c - 1; o++) if ((s += n[o].weight) < r && s + n[o + 1].weight >= r) {
                l = o + 1;
                break;
            }
            var d = n[l];
            t.forEach(function (t) {
                t.config_id == d.id && (e.currentWebPage = t);
            });
        }
    }
})
